package com.hd123.rumba.gwt.widget2.client.grid.renderer;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Label;
import com.hd123.rumba.gwt.widget2.client.form.HasNumberFormat;

/**
 * 数值标签渲染器
 * 
 * @author LiQi
 * 
 */
public class RNumberRenderer extends Label implements RCellRenderer, HasNumberFormat {

  /** 数值格式 */
  private NumberFormat format;

  /** 数值 */
  private Double value;

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    if (value == null)
      this.value = null;
    if (value instanceof Double)
      this.value = (Double) value;
    refreshText();
  }

  /**
   * 数值格式
   * 
   * @return
   */
  public NumberFormat getFormat() {
    return format;
  }

  /**
   * 设置数值格式
   * 
   * @param format
   */
  public void setFormat(NumberFormat format) {
    this.format = format;
    refreshText();
  }

  /**
   * 根据数值和数值格式刷新显示的字符串
   * 
   */
  private void refreshText() {
    if (value == null)
      setText("");
    else if (format == null)
      setText(value.toString());
    else
      setText(format.format(value.doubleValue()));

  }

}
